package com.acaslless.datasource.domain;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 分页查询参数
 *
 * @author haohan
 * @date 2024/11/1 14:31
 **/
@Data
@NoArgsConstructor
public class PageQuery<T> implements Serializable {

    private static final long serialVersionUID = -9087022168005779593L;

    private int pageNum = 1;

    private int pageSize = 10;

    private List<OrderItem> orders = new ArrayList<>();

    private T entity;

    public PageQuery(int pageNum, int pageSize, T entity) {
        this.pageNum = pageNum;
        this.pageSize = pageSize;
        this.entity = entity;
    }

    public <E> Page<E> build(Class<E> clazz) {
        Page<E> page = new Page<>(this.getPageNum(), this.getPageSize());
        if (CollUtil.isNotEmpty(getOrders())) {
            page.addOrder(getOrders());
        }
        return page;
    }
}
